package com.aiguigu.test;

public class ProCus {
    public static void main(String[] args) {
        Clear clear = new Clear();
          new Thread(new Runnable() {
              @Override
              public void run() {
                  for (;;){
                      try {
                          Thread.sleep(100);
                          clear.Producer();
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }

                  }
              }
          },"aaa").start();

          new Thread(new Runnable() {
              @Override
              public void run() {
                  for (;;){
                      try {
                          Thread.sleep(500);
                          clear.Customer();
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }

                  }
              }
          },"bbb").start();
    }
}

class Clear{
    private Integer ProduceNumber=0;

    public synchronized  void Producer(){
        if (ProduceNumber<20){
            ProduceNumber++;
            notify();
            System.out.println(Thread.currentThread().getName()+"开始生产地"+ProduceNumber+"个产品");
        }else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void Customer(){
        if (ProduceNumber>0){
            System.out.println(Thread.currentThread().getName()+"开始消费"+ProduceNumber+"个产品");
            ProduceNumber--;
            notify();
        }else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

